package com.ask.web;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ask.model.UserAccount;
import com.ask.service.impl.UserAccountServiceImpl;

@Controller
@RequestMapping(value="/userAccount")
public class UserAccountController {
	private static Log log = LogFactory.getLog(UserAccountController.class);
	
	@Autowired
	private UserAccountServiceImpl userAccountService;
	
	@RequestMapping(value="/forwardEmailRegister")
	public String forwardEmailRegister(){
		return "/pages/user/register.jsp";
	} 
	@RequestMapping(value="/forwardMobileRegister")
	public String forwardMobileRegister(){
		return "/pages/user/mobile_register.jsp";
	}
	
	@RequestMapping(value="/emailRegister" ,method=RequestMethod.POST)
	public String emailRegister(@ModelAttribute UserAccount userAccount,
		@RequestParam(value="imgFile",required=false) MultipartFile imgFile,
		ModelMap model) throws IOException{
		
		if(imgFile.getSize() >0){
			String fileName = imgFile.getOriginalFilename();
			File file = new File("D:/workspace/ask2/src/main/webapp/static/userImgHead/"+fileName);
			FileOutputStream fout = new FileOutputStream(file);
			fout.write(imgFile.getBytes());
			fout.close();
			
			userAccount.setHeadImg(fileName);
		}
		userAccountService.insert(userAccount);
		model.addAttribute("msg","注册成功，请登录");
		return "/pages/user/login.jsp";
	}
	
	@RequestMapping(value="/mobileRegister")
	public String mobileRegister(@ModelAttribute UserAccount userAccount,ModelMap model){
		userAccountService.insert(userAccount);
		model.addAttribute("msg","注册成功，请登录");
		return "/pages/user/login.jsp";
	}

	@RequestMapping(value="/login")
	public String login(@RequestParam String account, @RequestParam String passwd, ModelMap model ){
		UserAccount user = null;
		if(account.indexOf("@")> -1){
			user = userAccountService.selectByEmail(account);
		}else{
			user = userAccountService.selectByMobile(account);
		}
	
		if(user ==null){
			model.addAttribute("msg","账户不存在");
			return "/pages/user/login.jsp";
		}
	
		if(account.indexOf("@")> -1){
			if(user.getEmail().equals(account) && user.getPasswd().equals(passwd)){
				model.addAttribute("msg","登录成功");
				return "/pages/user/login.jsp";
			}else{
				model.addAttribute("msg","用户名或密码不正确");
				return "/pages/user/login.jsp";
			}
		}else{
			if(user.getMobile().equals(account) && user.getPasswd().equals(passwd)){
				model.addAttribute("msg","登录成功");
				return "/pages/user/login.jsp";
			}else{
				model.addAttribute("msg","用户名或密码不正确");
				return "/pages/user/login.jsp";
			}
		}
	}
}
